package com.ftg.learn.mybatis.chapter3.mapper;

import org.apache.ibatis.jdbc.SQL;

import java.util.Objects;

/**
 * @author kangjx
 * @createTime 2022/10/13
 */
public class SaleProvider {

    /**
     * 为我们生成动态sql
     * @return sql
     */
    public String createSqlBySelect2WithDyname(Long id,String name){
        StringBuilder sql = new StringBuilder();
        sql.append("select id, name, address, fid, unit, price, type, scdate, ccdate from sale where 1 = 1");
        if(Objects.nonNull(id)){
            sql.append(" and id = #{id}");
        }
        if(Objects.nonNull(name)){
            sql.append(" and name like concat(#{name},'%')");
        }
        sql.append(" and flag = 0");
        return sql.toString();
    }

    /**
     * 完成采用的是sql构建器，来完成对我们方法的调用
     * @param id    主键
     * @param name  名称
     * @return  String
     */
    public String createSqlBySelect2WithDyname1(Long id,String name){
        SQL sql = new SQL();
        sql.SELECT("id, name, address, fid, unit, price, type, scdate, ccdate")
                .FROM("sale")
                .WHERE(" flag =1 ");
        if(Objects.nonNull(id)){
            sql.AND().WHERE("id = #{id}");
        }
        if(Objects.nonNull(name)){
            sql.AND().WHERE("name like concat(#{name},'%')");
        }
        sql.ORDER_BY("id");
        return sql.toString();
    }

}
